package jia;

import java.util.logging.Level;

import env.WorldModel;

import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.Term;
import arch.RescuerArch;

public class obstacleB extends DefaultInternalAction {
    
    @Override
    public Object execute(TransitionSystem ts, Unifier un, Term[] terms) throws Exception {
        try {
            WorldModel model = ((RescuerArch)ts.getUserAgArch()).getModel();
            int x = (int)((NumberTerm)terms[0]).solve(); 
            int y = (int)((NumberTerm)terms[1]).solve();
            int ix = (int)((NumberTerm)terms[2]).solve();
            int iy = (int)((NumberTerm)terms[3]).solve();
            int fx = (int)((NumberTerm)terms[4]).solve();
            int fy = (int)((NumberTerm)terms[5]).solve();
            
            if(!estaDentro(x, y, ix, iy, fx, fy))
            	return true;
            
            return !model.inGrid(x,y) || model.hasObject(WorldModel.OBSTACLE, x, y);
        } catch (Throwable e) {
            ts.getLogger().log(Level.SEVERE, "jia.obstacle error: "+e, e);          
        }
        return false;
    }
    
    private static boolean estaDentro(int x, int y, int ix, int iy, int fx, int fy){
    	if (x >= ix && x <= fx && y >= iy && y <= fy)
    		return true;
    	return false;
    }
}

